---
title: "Supporting Veterans: Source Cues, Issue Ownership, and the Electoral Benefits of Military Service"
author: Peter T. McLaughlin, Sarina Rhinehart, & Matthew J. Geras
---

```{r Setup, include=FALSE, warning=FALSE, message=FALSE}
knitr::opts_chunk$set(echo = FALSE)

library(car)
library(psych)
library(tidyr)
library(dplyr)
library(janitor)
library(ggplot2)
library(stargazer)
library(cowplot)
library(estimatr) # Cluster SE
library(MASS) # ordered logit
library(stringr)
library(rio)
library(broom)
library(purrr)
library(flextable)
library(modelsummary)

options(scipen=999)

# Set Local Working Directory
setwd()

## Load in data
dsorg <- import("Vets_Experiment_2_Data.RData") 


# Consent
table(dsorg$consent)
ds <- subset(dsorg, consent=="I agree to participate") 

```


```{r Treatment variable}

# Create Variable for treatment Group
table(ds$Dem_Vet_Exp_DO, useNA="always") 
ds$treatment_group <- NA

ds$treatment_group[ds$Dem_Vet_Exp_DO=="d_nonvet_deficit|d_therm|d_traits|d_issue_own|d_cand_ideology|d_vote|d_manip"]="nonvet_def"
ds$treatment_group[ds$Dem_Vet_Exp_DO=="d_nonvet_ns|d_therm|d_traits|d_issue_own|d_cand_ideology|d_vote|d_manip"]= "nonvet_ns"
ds$treatment_group[ds$Dem_Vet_Exp_DO=="d_nonvet_poverty|d_therm|d_traits|d_issue_own|d_cand_ideology|d_vote|d_manip"]="nonvet_pov"
ds$treatment_group[ds$Dem_Vet_Exp_DO=="d_vet_deficit|d_therm|d_traits|d_issue_own|d_cand_ideology|d_vote|d_manip"]="vet_def"
ds$treatment_group[ds$Dem_Vet_Exp_DO=="d_vet_ns|d_therm|d_traits|d_issue_own|d_cand_ideology|d_vote|d_manip"]="vet_ns"
ds$treatment_group[ds$Dem_Vet_Exp_DO=="d_vet_poverty|d_therm|d_traits|d_issue_own|d_cand_ideology|d_vote|d_manip"]="vet_pov"

table(ds$Rep_Vet_Exp_DO, useNA="always") 
ds$treatment_group[ds$Rep_Vet_Exp_DO=="nonvet_deficit|therm|traits|issue_own|cand_ideology|vote|manip"]="nonvet_def"
ds$treatment_group[ds$Rep_Vet_Exp_DO=="nonvet_ns|therm|traits|issue_own|cand_ideology|vote|manip"]= "nonvet_ns"
ds$treatment_group[ds$Rep_Vet_Exp_DO=="nonvet_poverty|therm|traits|issue_own|cand_ideology|vote|manip"]="nonvet_pov"
ds$treatment_group[ds$Rep_Vet_Exp_DO=="vet_deficit|therm|traits|issue_own|cand_ideology|vote|manip"]="vet_def"
ds$treatment_group[ds$Rep_Vet_Exp_DO=="vet_ns|therm|traits|issue_own|cand_ideology|vote|manip"]="vet_ns"
ds$treatment_group[ds$Rep_Vet_Exp_DO=="vet_poverty|therm|traits|issue_own|cand_ideology|vote|manip"]="vet_pov"

ds$treatment_group_f <- factor(ds$treatment_group)
ds$treatment_group_f = relevel(ds$treatment_group_f, ref="vet_ns")

# Create two treatment dimensions
ds$treatment_vet_status <- NA
ds$treatment_vet_status[ds$treatment_group=="vet_def"]="veteran"
ds$treatment_vet_status[ds$treatment_group=="vet_ns"]= "veteran"
ds$treatment_vet_status[ds$treatment_group=="vet_pov"]= "veteran"
ds$treatment_vet_status[ds$treatment_group=="nonvet_def"]="nonveteran"
ds$treatment_vet_status[ds$treatment_group=="nonvet_ns"]= "nonveteran"
ds$treatment_vet_status[ds$treatment_group=="nonvet_pov"]= "nonveteran"

ds$treatment_issue <- NA
ds$treatment_issue[ds$treatment_group=="vet_def"]="deficit"
ds$treatment_issue[ds$treatment_group=="vet_ns"]= "nat_sec"
ds$treatment_issue[ds$treatment_group=="vet_pov"]= "poverty"
ds$treatment_issue[ds$treatment_group=="nonvet_def"]="deficit"
ds$treatment_issue[ds$treatment_group=="nonvet_ns"]= "nat_sec"
ds$treatment_issue[ds$treatment_group=="nonvet_pov"]= "poverty"
```


```{r DVs}
# Feeling thermometer, issue ownership, and trait ownership
ds <- ds %>% 
mutate(therm = coalesce(therm_1, d_therm_1),
       defense_own = coalesce(issue_own_1, d_issue_own_1),
       patriotism = coalesce(traits_1, d_traits_1),
       courage = coalesce(traits_2, d_traits_2))

ds %>% drop_na(therm) -> ds
```



```{r Demographic variables}
# Education
table(ds$education, useNA="always")
ds$education2 <- car::recode(ds$education, "1=1; 2=2; 3=3; 4=3; 5=4; 6=5;7=6; 8=6; else=NA")
table(ds$education2, useNA="always")
ds$education_f <- as.integer(ds$education2) 

#Gender
table(ds$gender, useNA="always")
ds$gender_f <- car::recode(ds$gender, "1='Man'; 2='Woman'; else=NA")

#Race
table(ds$race, useNA="always")
ds$race2 <- NA
ds$race2[ds$race=="White"]="White"
ds$race2[ds$race=="Asian American"]="Asian American"
ds$race2[ds$race=="Black or African American"]="Black"
ds$race2[ds$race=="Hispanic or Latino(a)"]="Latino"
ds$race2[str_detect(ds$race, ",")]= "Mixed"
ds$race2[ds$race=="Native American or American Indian"]="Other"
ds$race2[ds$race=="Other"]="Other"
table(ds$race2, useNA="always") 
ds$race_f <- factor(ds$race2, ordered=FALSE)

#Ideology
table(ds$ideo, useNA="always")
ds$ideology2 <- car::recode(ds$ideo, "'Very liberal'=1;
                           'Liberal'=2; 'Slightly liberal'=3; 'Moderate'=4;
                            'Slightly conservative'=5; 'Conservative'=6; 'Very conservative'=7;
                           else=NA")
ds$ideology_f <- as.integer(ds$ideology2)

#Income
table(ds$income, useNA="always")
ds$income2 <- car::recode(ds$income, "'Under $10,000'=0; '$10,000-$19,999'=1;
                              '$20,000-$29,999'=2; '$30,000-$39,999'=3; '$40,000-$49,999'=4;
                              '$50,000-$59,999'=5; '$60,000-$74,999'=6; '$75,000-$89,999'=7;
                              '$90,000-$119,999'=8; '$120,000-$159,999'=9; '$160,000-$200,000'= 10;
                              'Over $200,000'=11;else=NA")
ds$income_f <- as.integer(ds$income2)

# Party
table(ds$party, useNA="always")
ds$party_f <- car::recode(ds$party, "'Independent'='Independent'; 'Not very strong Democrat'='Democrat';
                              'Not very strong Republican'='Republican'; 'Strong Democrat'='Democrat';
                             'Strong Republican'='Republican'; else=NA")
ds$party2_f <- NA
ds$party2_f[ds$party_f=="Democrat"]="Democrat"
ds$party2_f[ds$party_f=="Republican"]="Republican"
ds$party2_f[ds$inds_party=="More similar to Democrats than Republicans"]="Democrat"
ds$party2_f[ds$inds_party=="More similar to Republicans than Democrats"]="Republican"
ds$party2_f = factor(ds$party2_f)
ds$party2_f = relevel(ds$party2_f, ref="Democrat")

# Candidate party
table(ds$FL_82_DO, useNA="always")
ds$candidate_party <- NA

ds$candidate_party[ds$FL_82_DO=="Rep_Vet_Exp"]="rep"
ds$candidate_party[ds$FL_82_DO=="Dem_Vet_Exp"]="dem"

table(ds$candidate_party, useNA="always") 

# Partisan Match variable
table(ds$candidate_party)
table(ds$party)

mutate(ds, partisan_match = ifelse(party_f == 'Independent', 0,
                                     ifelse(party_f == 'Democrat' & candidate_party == 'dem', 1,
                                            ifelse(party_f == 'Republican' & candidate_party == 'rep', 1, -1)))) -> ds



# Religion
table(ds$religion, useNA="always")
ds$religion <- car::recode(ds$religion, "'Never'=1; 'A few times a year'=2; 'Once or twice a month'=3;
                              'Almost every week'=4; 'Every week'=5; else=NA")
table(ds$religion, useNA="always")
ds$religion_f <- as.integer(ds$religion)
table(ds$religion_f, useNA="always")

# Age
ds$age_f <- ds$age
describe(ds$age_f)

# Region
table(ds$region) # 1= Northeast; 2= Midwest; 3= South; 4= West
ds$region_f <- ds$region
```


```{r Sample demographics}

tabyl(ds$education_f, sort=T) 

tabyl(ds$gender_f, sort=T) 

tabyl(ds$race_f, sort=T) 

tabyl(ds$ideology_f, sort=T) 

describe(ds$income_f) 

tabyl(ds$party_f, sort=T) 

tabyl(ds$religion_f, sort=T)  

describe(ds$age_f) 

tabyl(ds$region_f) 

```


```{r Feeling Thermometer Fig}

# Veteran National Security
ds_vet_ns <- subset(ds, treatment_group_f=="vet_ns")
test_vet_ns <- t.test(ds_vet_ns$therm)
vet_ns_mean <- test_vet_ns$estimate
vet_ns_lower <- test_vet_ns$conf.int[1]
vet_ns_upper <- test_vet_ns$conf.int[2]

# Veteran def
ds_vet_def <- subset(ds, treatment_group_f=="vet_def")
test_vet_def <- t.test(ds_vet_def$therm)
vet_def_mean <- test_vet_def$estimate
vet_def_lower <- test_vet_def$conf.int[1]
vet_def_upper <- test_vet_def$conf.int[2]

# Veteran pov
ds_vet_pov <- subset(ds, treatment_group_f=="vet_pov")
test_vet_pov <- t.test(ds_vet_pov$therm)
vet_pov_mean <- test_vet_pov$estimate
vet_pov_lower <- test_vet_pov$conf.int[1]
vet_pov_upper <- test_vet_pov$conf.int[2]

# Nonveteran National Security
ds_nonvet_ns <- subset(ds, treatment_group_f=="nonvet_ns")
test_nonvet_ns <- t.test(ds_nonvet_ns$therm)
nonvet_ns_mean <- test_nonvet_ns$estimate
nonvet_ns_lower <- test_nonvet_ns$conf.int[1]
nonvet_ns_upper <- test_nonvet_ns$conf.int[2]

# Nonveteran def
ds_nonvet_def <- subset(ds, treatment_group_f=="nonvet_def")
test_nonvet_def <- t.test(ds_nonvet_def$therm)
nonvet_def_mean <- test_nonvet_def$estimate
nonvet_def_lower <- test_nonvet_def$conf.int[1]
nonvet_def_upper <- test_nonvet_def$conf.int[2]

# Nonveteran pov
ds_nonvet_pov <- subset(ds, treatment_group_f=="nonvet_pov")
test_nonvet_pov <- t.test(ds_nonvet_pov$therm)
nonvet_pov_mean <- test_nonvet_pov$estimate
nonvet_pov_lower <- test_nonvet_pov$conf.int[1]
nonvet_pov_upper <- test_nonvet_pov$conf.int[2]


means <- c(vet_ns_mean, vet_def_mean, vet_pov_mean,
           nonvet_ns_mean, nonvet_def_mean, nonvet_pov_mean)
lower <- c(vet_ns_lower, vet_def_lower, vet_pov_lower,
           nonvet_ns_lower, nonvet_def_lower, nonvet_pov_lower)
upper <- c(vet_ns_upper, vet_def_upper, vet_pov_upper,
           nonvet_ns_upper, nonvet_def_upper, nonvet_pov_upper)
vet_group <- c("Veteran","Veteran", "Veteran", "Nonveteran","Nonveteran", "Nonveteran")
issue_group <- c("National Defense","Federal Deficit", "Poverty", "National Defense","Federal Deficit", "Poverty")
therm_condensed_d <- (cbind(means, lower, upper, vet_group, issue_group))
therm_condensed_d <- as.data.frame(therm_condensed_d)
therm_condensed_d$means <- as.numeric(as.character(therm_condensed_d$means))
therm_condensed_d$lower <- as.numeric(as.character(therm_condensed_d$lower))
therm_condensed_d$upper <- as.numeric(as.character(therm_condensed_d$upper))

ggplot(therm_condensed_d, aes(x=issue_group, y=means, color=vet_group)) +
  geom_point(size= 2, position=position_dodge(width = .6), stat="identity") +
  geom_errorbar(aes(ymin=lower, ymax=upper),
                width=.5,
                size = 1,
                position=position_dodge(.6)) +
  scale_x_discrete(guide = guide_axis(angle = 20)) +
  labs(title= "All Respondents", x ="", y = "Mean Feeling Therm. Rating \n")+
  scale_y_continuous(limits=c(55, 85))+
  scale_color_manual(values=c("gray0", "gray55"), labels = c("Nonveteran", "Veteran")) +
  theme_minimal()+
  theme(legend.position="top")+
  theme(legend.title=element_blank()) +
  theme(text = element_text(size=16, family = ("serif"))) -> therm_fig
```

```{r Defense Owbership Fig}

# Veteran National Security
ds_vet_ns <- subset(ds, treatment_group_f=="vet_ns")
test_vet_ns <- t.test(ds_vet_ns$defense_own)
vet_ns_mean <- test_vet_ns$estimate
vet_ns_lower <- test_vet_ns$conf.int[1]
vet_ns_upper <- test_vet_ns$conf.int[2]

# Veteran def
ds_vet_def <- subset(ds, treatment_group_f=="vet_def")
test_vet_def <- t.test(ds_vet_def$defense_own)
vet_def_mean <- test_vet_def$estimate
vet_def_lower <- test_vet_def$conf.int[1]
vet_def_upper <- test_vet_def$conf.int[2]

# Veteran pov
ds_vet_pov <- subset(ds, treatment_group_f=="vet_pov")
test_vet_pov <- t.test(ds_vet_pov$defense_own)
vet_pov_mean <- test_vet_pov$estimate
vet_pov_lower <- test_vet_pov$conf.int[1]
vet_pov_upper <- test_vet_pov$conf.int[2]

# Nonveteran National Security
ds_nonvet_ns <- subset(ds, treatment_group_f=="nonvet_ns")
test_nonvet_ns <- t.test(ds_nonvet_ns$defense_own)
nonvet_ns_mean <- test_nonvet_ns$estimate
nonvet_ns_lower <- test_nonvet_ns$conf.int[1]
nonvet_ns_upper <- test_nonvet_ns$conf.int[2]

# Nonveteran def
ds_nonvet_def <- subset(ds, treatment_group_f=="nonvet_def")
test_nonvet_def <- t.test(ds_nonvet_def$defense_own)
nonvet_def_mean <- test_nonvet_def$estimate
nonvet_def_lower <- test_nonvet_def$conf.int[1]
nonvet_def_upper <- test_nonvet_def$conf.int[2]

# Nonveteran pov
ds_nonvet_pov <- subset(ds, treatment_group_f=="nonvet_pov")
test_nonvet_pov <- t.test(ds_nonvet_pov$defense_own)
nonvet_pov_mean <- test_nonvet_pov$estimate
nonvet_pov_lower <- test_nonvet_pov$conf.int[1]
nonvet_pov_upper <- test_nonvet_pov$conf.int[2]


means <- c(vet_ns_mean, vet_def_mean, vet_pov_mean,
           nonvet_ns_mean, nonvet_def_mean, nonvet_pov_mean)
lower <- c(vet_ns_lower, vet_def_lower, vet_pov_lower,
           nonvet_ns_lower, nonvet_def_lower, nonvet_pov_lower)
upper <- c(vet_ns_upper, vet_def_upper, vet_pov_upper,
           nonvet_ns_upper, nonvet_def_upper, nonvet_pov_upper)
vet_group <- c("Veteran","Veteran", "Veteran", "Nonveteran","Nonveteran", "Nonveteran")
issue_group <- c("National Defense","Federal Deficit", "Poverty", "National Defense","Federal Deficit", "Poverty")
therm_condensed_d <- (cbind(means, lower, upper, vet_group, issue_group))
therm_condensed_d <- as.data.frame(therm_condensed_d)
therm_condensed_d$means <- as.numeric(as.character(therm_condensed_d$means))
therm_condensed_d$lower <- as.numeric(as.character(therm_condensed_d$lower))
therm_condensed_d$upper <- as.numeric(as.character(therm_condensed_d$upper))

ggplot(therm_condensed_d, aes(x=issue_group, y=means, color=vet_group)) +
  geom_point(size= 2, position=position_dodge(width = .6), stat="identity") +
  geom_errorbar(aes(ymin=lower, ymax=upper),
                width=.5,
                size = 1,
                position=position_dodge(.6)) +
  scale_x_discrete(guide = guide_axis(angle = 20)) +
        scale_y_continuous(limits=c(4.5, 9))+
  labs(title = "All Respondents", x ="", y = "Mean Defense Own. Rating \n")+
  scale_color_manual(values=c("gray0", "gray55"), labels = c("Nonveteran", "Veteran")) +
  theme_minimal()+
  #theme(legend.justification=c(0,0), legend.position=c(0,.8))+
  theme(legend.position="top")+
  theme(legend.title=element_blank()) +
  theme(text = element_text(size=16, family = ("serif"))) -> defense_own_fig
```

```{r Patriotism Fig}

# Veteran National Security
ds_vet_ns <- subset(ds, treatment_group_f=="vet_ns")
test_vet_ns <- t.test(ds_vet_ns$patriotism)
vet_ns_mean <- test_vet_ns$estimate
vet_ns_lower <- test_vet_ns$conf.int[1]
vet_ns_upper <- test_vet_ns$conf.int[2]

# Veteran def
ds_vet_def <- subset(ds, treatment_group_f=="vet_def")
test_vet_def <- t.test(ds_vet_def$patriotism)
vet_def_mean <- test_vet_def$estimate
vet_def_lower <- test_vet_def$conf.int[1]
vet_def_upper <- test_vet_def$conf.int[2]

# Veteran pov
ds_vet_pov <- subset(ds, treatment_group_f=="vet_pov")
test_vet_pov <- t.test(ds_vet_pov$patriotism)
vet_pov_mean <- test_vet_pov$estimate
vet_pov_lower <- test_vet_pov$conf.int[1]
vet_pov_upper <- test_vet_pov$conf.int[2]

# Nonveteran National Security
ds_nonvet_ns <- subset(ds, treatment_group_f=="nonvet_ns")
test_nonvet_ns <- t.test(ds_nonvet_ns$patriotism)
nonvet_ns_mean <- test_nonvet_ns$estimate
nonvet_ns_lower <- test_nonvet_ns$conf.int[1]
nonvet_ns_upper <- test_nonvet_ns$conf.int[2]

# Nonveteran def
ds_nonvet_def <- subset(ds, treatment_group_f=="nonvet_def")
test_nonvet_def <- t.test(ds_nonvet_def$patriotism)
nonvet_def_mean <- test_nonvet_def$estimate
nonvet_def_lower <- test_nonvet_def$conf.int[1]
nonvet_def_upper <- test_nonvet_def$conf.int[2]

# Nonveteran pov
ds_nonvet_pov <- subset(ds, treatment_group_f=="nonvet_pov")
test_nonvet_pov <- t.test(ds_nonvet_pov$patriotism)
nonvet_pov_mean <- test_nonvet_pov$estimate
nonvet_pov_lower <- test_nonvet_pov$conf.int[1]
nonvet_pov_upper <- test_nonvet_pov$conf.int[2]


means <- c(vet_ns_mean, vet_def_mean, vet_pov_mean,
           nonvet_ns_mean, nonvet_def_mean, nonvet_pov_mean)
lower <- c(vet_ns_lower, vet_def_lower, vet_pov_lower,
           nonvet_ns_lower, nonvet_def_lower, nonvet_pov_lower)
upper <- c(vet_ns_upper, vet_def_upper, vet_pov_upper,
           nonvet_ns_upper, nonvet_def_upper, nonvet_pov_upper)
vet_group <- c("Veteran","Veteran", "Veteran", "Nonveteran","Nonveteran", "Nonveteran")
issue_group <- c("National Defense","Federal Deficit", "Poverty", "National Defense","Federal Deficit", "Poverty")
therm_condensed_d <- (cbind(means, lower, upper, vet_group, issue_group))
therm_condensed_d <- as.data.frame(therm_condensed_d)
therm_condensed_d$means <- as.numeric(as.character(therm_condensed_d$means))
therm_condensed_d$lower <- as.numeric(as.character(therm_condensed_d$lower))
therm_condensed_d$upper <- as.numeric(as.character(therm_condensed_d$upper))

ggplot(therm_condensed_d, aes(x=issue_group, y=means, color=vet_group)) +
  geom_point(size= 2, position=position_dodge(width = .6), stat="identity") +
  geom_errorbar(aes(ymin=lower, ymax=upper),
                width=.5,
                size = 1,
                position=position_dodge(.6)) +
  scale_x_discrete(guide = guide_axis(angle = 20)) +
        scale_y_continuous(limits=c(5, 8))+
  labs(title= "Patriotism", x ="Issue Discussed", y = "Mean Trait Rating \n")+
  scale_color_manual(values=c("gray0", "gray55"), labels = c("Nonveteran", "Veteran")) +
  theme_minimal()+
  #theme(legend.justification=c(0,0), legend.position=c(0,.8))+
  theme(legend.position="top")+
  theme(legend.title=element_blank()) +
  theme(text = element_text(size=16, family = ("serif"))) -> patriotism_fig
```

```{r Courage Fig}

# Veteran National Security
ds_vet_ns <- subset(ds, treatment_group_f=="vet_ns")
test_vet_ns <- t.test(ds_vet_ns$courage)
vet_ns_mean <- test_vet_ns$estimate
vet_ns_lower <- test_vet_ns$conf.int[1]
vet_ns_upper <- test_vet_ns$conf.int[2]

# Veteran def
ds_vet_def <- subset(ds, treatment_group_f=="vet_def")
test_vet_def <- t.test(ds_vet_def$courage)
vet_def_mean <- test_vet_def$estimate
vet_def_lower <- test_vet_def$conf.int[1]
vet_def_upper <- test_vet_def$conf.int[2]

# Veteran pov
ds_vet_pov <- subset(ds, treatment_group_f=="vet_pov")
test_vet_pov <- t.test(ds_vet_pov$courage)
vet_pov_mean <- test_vet_pov$estimate
vet_pov_lower <- test_vet_pov$conf.int[1]
vet_pov_upper <- test_vet_pov$conf.int[2]

# Nonveteran National Security
ds_nonvet_ns <- subset(ds, treatment_group_f=="nonvet_ns")
test_nonvet_ns <- t.test(ds_nonvet_ns$courage)
nonvet_ns_mean <- test_nonvet_ns$estimate
nonvet_ns_lower <- test_nonvet_ns$conf.int[1]
nonvet_ns_upper <- test_nonvet_ns$conf.int[2]

# Nonveteran def
ds_nonvet_def <- subset(ds, treatment_group_f=="nonvet_def")
test_nonvet_def <- t.test(ds_nonvet_def$courage)
nonvet_def_mean <- test_nonvet_def$estimate
nonvet_def_lower <- test_nonvet_def$conf.int[1]
nonvet_def_upper <- test_nonvet_def$conf.int[2]

# Nonveteran pov
ds_nonvet_pov <- subset(ds, treatment_group_f=="nonvet_pov")
test_nonvet_pov <- t.test(ds_nonvet_pov$courage)
nonvet_pov_mean <- test_nonvet_pov$estimate
nonvet_pov_lower <- test_nonvet_pov$conf.int[1]
nonvet_pov_upper <- test_nonvet_pov$conf.int[2]


means <- c(vet_ns_mean, vet_def_mean, vet_pov_mean,
           nonvet_ns_mean, nonvet_def_mean, nonvet_pov_mean)
lower <- c(vet_ns_lower, vet_def_lower, vet_pov_lower,
           nonvet_ns_lower, nonvet_def_lower, nonvet_pov_lower)
upper <- c(vet_ns_upper, vet_def_upper, vet_pov_upper,
           nonvet_ns_upper, nonvet_def_upper, nonvet_pov_upper)
vet_group <- c("Veteran","Veteran", "Veteran", "Nonveteran","Nonveteran", "Nonveteran")
issue_group <- c("National Defense","Federal Deficit", "Poverty", "National Defense","Federal Deficit", "Poverty")
therm_condensed_d <- (cbind(means, lower, upper, vet_group, issue_group))
therm_condensed_d <- as.data.frame(therm_condensed_d)
therm_condensed_d$means <- as.numeric(as.character(therm_condensed_d$means))
therm_condensed_d$lower <- as.numeric(as.character(therm_condensed_d$lower))
therm_condensed_d$upper <- as.numeric(as.character(therm_condensed_d$upper))

ggplot(therm_condensed_d, aes(x=issue_group, y=means, color=vet_group)) +
  geom_point(size= 2, position=position_dodge(width = .6), stat="identity") +
  geom_errorbar(aes(ymin=lower, ymax=upper),
                width=.5,
                size = 1,
                position=position_dodge(.6)) +
  scale_x_discrete(guide = guide_axis(angle = 20)) +
      scale_y_continuous(limits=c(5, 8))+
  labs(title= "Courage", x ="Issue Discussed", y = "")+
  scale_color_manual(values=c("gray0", "gray55"), labels = c("Nonveteran", "Veteran")) +
  theme_minimal()+
  #theme(legend.justification=c(0,0), legend.position=c(0,.8))+
  theme(legend.position="top")+
  theme(legend.title=element_blank()) +
  theme(text = element_text(size=16, family = ("serif"))) -> courage_fig
```


```{r T-tests}

ds_vets <- subset(ds, treatment_group_f %in% c("vet_ns","vet_pov"))

t.test(ds_vets$therm ~ ds_vets$treatment_group_f) -> vets_therm_pov
t.test(ds_vets$defense_own ~ ds_vets$treatment_group_f) -> vets_own_pov
t.test(ds_vets$patriotism ~ ds_vets$treatment_group_f) -> vets_patriotism_pov
t.test(ds_vets$courage ~ ds_vets$treatment_group_f) -> vets_courage_pov

ds_vets_2 <- subset(ds, treatment_group_f %in% c("vet_ns","vet_def"))

t.test(ds_vets_2$therm ~ ds_vets_2$treatment_group_f) -> vets_therm_def
t.test(ds_vets_2$defense_own ~ ds_vets_2$treatment_group_f) -> vets_own_def
t.test(ds_vets_2$patriotism ~ ds_vets_2$treatment_group_f) -> vets_patriotism_def
t.test(ds_vets_2$courage ~ ds_vets_2$treatment_group_f) -> vets_courage_def


ds_nat_sec <- subset(ds, treatment_group_f %in% c("vet_ns","nonvet_ns"))

t.test(ds_nat_sec$therm ~ ds_nat_sec$treatment_group_f) -> ns_therm
t.test(ds_nat_sec$defense_own ~ ds_nat_sec$treatment_group_f) -> ns_issue_own
t.test(ds_nat_sec$patriotism ~ ds_nat_sec$treatment_group_f) -> ns_patriotism
t.test(ds_nat_sec$courage ~ ds_nat_sec$treatment_group_f) -> ns_courage

## Tables
# Set Function
pvalue_format <- function(x){
  z <- cut(as.numeric(x), breaks = c(-Inf, 0.01, 0.05, 0.1, Inf), labels = c("***", "**", "*", ""))
  as.character(z)
}

# Vets only Def
tab_vets_def <- map_df(list(vets_therm_def, vets_own_def, vets_patriotism_def, vets_courage_def), .id = "dv", tidy)  
table_vets_def <- tab_vets_def[c("dv", "estimate", "estimate1", "estimate2", "p.value")]

  flextable(table_vets_def,
    col_keys = c("dv", "estimate", "estimate1", "estimate2", "p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", estimate = "Diff. of Means",
                    estimate1 = "National Defense", estimate2 = "Deficit",
                    p.value = "Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableB1

# Vets only POV
tab_vets_pov <- map_df(list(vets_therm_pov, vets_own_pov, vets_patriotism_pov, vets_courage_pov), .id = "dv", tidy)  
table_vets_pov <- tab_vets_pov[c("dv", "estimate", "estimate1", "estimate2", "p.value")]

  flextable(table_vets_pov,
    col_keys = c("dv", "estimate", "estimate1", "estimate2", "p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", estimate = "Diff. of Means",
                    estimate1 = "National Defense", estimate2 = "Poverty",
                    p.value = "Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableB2


# National Security only 
tab_ns <- map_df(list(ns_therm, ns_issue_own, ns_patriotism, ns_courage), .id = "dv", tidy)  
table_ns <- tab_ns[c("dv", "estimate", "estimate1", "estimate2", "p.value")]

  flextable(table_ns,
    col_keys = c("dv", "estimate", "estimate1", "estimate2", "p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", estimate = "Diff. of Means",
                    estimate1 = "Veteran", estimate2 = "Nonveteran",
                    p.value = "Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableB3

```

```{r T-test with tukey comparisions}

#remove nonvet crime
ds_anova <- subset(ds, treatment_group_f!="nonvet_pov" & treatment_group_f!="nonvet_def")

#therm
therm_anova <- aov(therm~treatment_group_f, data=ds_anova)
summary(therm_anova)
TukeyHSD(therm_anova, conf.level=.95) -> tukey_therm

#defense own
defense_own_anova <- aov(defense_own~treatment_group_f, data=ds_anova)
summary(defense_own_anova)
TukeyHSD(defense_own_anova, conf.level=.95) -> tukey_def_own

#patriotism
patriotism_anova <- aov(patriotism~treatment_group_f, data=ds_anova)
summary(patriotism_anova)
TukeyHSD(patriotism_anova, conf.level=.95) -> tukey_patriotism

#courage
courage_anova <- aov(courage~treatment_group_f, data=ds_anova)
summary(courage_anova)
TukeyHSD(courage_anova, conf.level=.95) -> tukey_courage

# Table
tukey_df <- map_df(list(tukey_therm, tukey_def_own, tukey_patriotism, tukey_courage), .id = "dv", tidy)
table_tukey <- tukey_df[c("dv", "contrast", "estimate", "adj.p.value")]

  flextable(table_tukey,
    col_keys = c("dv", "contrast", "estimate", "adj.p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(adj.p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", 
                    contrast = "Contrast", estimate = "Diff. of Means",
                    adj.p.value = "Adjusted Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "adj.p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableB10
```


```{r  Models With Party Match}

mod_therm_pm <- lm(therm ~ treatment_group_f  +  partisan_match, ds)
summary(mod_therm_pm)

mod_defense_own_pm <- lm(defense_own ~ treatment_group_f + partisan_match, ds)
summary(mod_defense_own_pm)

mod_patriotism_pm <- lm(patriotism ~ treatment_group_f + partisan_match, ds)
summary(mod_patriotism_pm)

mod_courage_pm <- lm(courage ~ treatment_group_f + partisan_match, ds)
summary(mod_courage_pm)

# Table
models_full <- list()
models_full[['Feeling Thermometer']] <- mod_therm_pm
models_full[['Defense Issue Ownership']] <- mod_defense_own_pm
models_full[['Patriotism']] <- mod_patriotism_pm
models_full[['Courage']] <- mod_courage_pm

coefs <- c("treatment_group_fnonvet_ns" = "Treatment: Nonvet. / Nat. Def.",
          "treatment_group_fvet_def" = "Treatment: Vet. / Deficit", 
          "treatment_group_fvet_pov" = "Treatment: Vet. / Poverty",
          "treatment_group_fnonvet_def" = "Treatment: Nonvet. / Deficit",
          "treatment_group_fnonvet_pov" = "Treatment: Nonvet. / Poverty",
          "partisan_match" = "Partisan Match",
          "(Intercept)" = "Constant")

modelsummary(models_full, 
             stars = c('*' = .1, '**' = .05, '***' = .01),
             coef_map = coefs,
             title = '', #output = "Table_Full.docx"
             ) -> TableB13

```


```{r Republicans Only Therm Fig}

ds_r <- ds %>%
  filter(ds$party_f == "Republican")

# Veteran National Security
ds_r_vet_ns <- subset(ds_r, treatment_group_f=="vet_ns")
test_vet_ns <- t.test(ds_r_vet_ns$therm)
vet_ns_mean <- test_vet_ns$estimate
vet_ns_lower <- test_vet_ns$conf.int[1]
vet_ns_upper <- test_vet_ns$conf.int[2]

# Veteran def
ds_r_vet_def <- subset(ds_r, treatment_group_f=="vet_def")
test_vet_def <- t.test(ds_r_vet_def$therm)
vet_def_mean <- test_vet_def$estimate
vet_def_lower <- test_vet_def$conf.int[1]
vet_def_upper <- test_vet_def$conf.int[2]

# Veteran pov
ds_r_vet_pov <- subset(ds_r, treatment_group_f=="vet_pov")
test_vet_pov <- t.test(ds_r_vet_pov$therm)
vet_pov_mean <- test_vet_pov$estimate
vet_pov_lower <- test_vet_pov$conf.int[1]
vet_pov_upper <- test_vet_pov$conf.int[2]

# Nonveteran National Security
ds_r_nonvet_ns <- subset(ds_r, treatment_group_f=="nonvet_ns")
test_nonvet_ns <- t.test(ds_r_nonvet_ns$therm)
nonvet_ns_mean <- test_nonvet_ns$estimate
nonvet_ns_lower <- test_nonvet_ns$conf.int[1]
nonvet_ns_upper <- test_nonvet_ns$conf.int[2]

# Nonveteran def
ds_r_nonvet_def <- subset(ds_r, treatment_group_f=="nonvet_def")
test_nonvet_def <- t.test(ds_r_nonvet_def$therm)
nonvet_def_mean <- test_nonvet_def$estimate
nonvet_def_lower <- test_nonvet_def$conf.int[1]
nonvet_def_upper <- test_nonvet_def$conf.int[2]

# Nonveteran pov
ds_r_nonvet_pov <- subset(ds_r, treatment_group_f=="nonvet_pov")
test_nonvet_pov <- t.test(ds_r_nonvet_pov$therm)
nonvet_pov_mean <- test_nonvet_pov$estimate
nonvet_pov_lower <- test_nonvet_pov$conf.int[1]
nonvet_pov_upper <- test_nonvet_pov$conf.int[2]


means <- c(vet_ns_mean, vet_def_mean, vet_pov_mean,
           nonvet_ns_mean, nonvet_def_mean, nonvet_pov_mean)
lower <- c(vet_ns_lower, vet_def_lower, vet_pov_lower,
           nonvet_ns_lower, nonvet_def_lower, nonvet_pov_lower)
upper <- c(vet_ns_upper, vet_def_upper, vet_pov_upper,
           nonvet_ns_upper, nonvet_def_upper, nonvet_pov_upper)
vet_group <- c("Veteran","Veteran", "Veteran", "Nonveteran","Nonveteran", "Nonveteran")
issue_group <- c("National Defense","Federal Deficit", "Poverty", "National Defense","Federal Deficit", "Poverty")
therm_condensed_d <- (cbind(means, lower, upper, vet_group, issue_group))
therm_condensed_d <- as.data.frame(therm_condensed_d)
therm_condensed_d$means <- as.numeric(as.character(therm_condensed_d$means))
therm_condensed_d$lower <- as.numeric(as.character(therm_condensed_d$lower))
therm_condensed_d$upper <- as.numeric(as.character(therm_condensed_d$upper))

ggplot(therm_condensed_d, aes(x=issue_group, y=means, color=vet_group)) +
  geom_point(size= 2, position=position_dodge(width = .6), stat="identity") +
  geom_errorbar(aes(ymin=lower, ymax=upper),
                width=.5,
                size = 1,
                position=position_dodge(.6)) +
  scale_x_discrete(guide = guide_axis(angle = 20)) +
    scale_y_continuous(limits=c(55, 85))+
  labs(title = "Republicans", x ="Issue Discussed", y = "")+
  scale_color_manual(values=c("gray0", "gray55"), labels = c("Nonveteran", "Veteran")) +
  theme_minimal()+
  #theme(legend.justification=c(0,0), legend.position=c(0,.8))+
  theme(legend.position="top")+
  theme(legend.title=element_blank()) +
  theme(text = element_text(size=16, family = ("serif")))-> therm_fig_r

```

```{r Republicans Only Defense Own Fig}

# Veteran National Security
ds_vet_ns <- subset(ds_r, treatment_group_f=="vet_ns")
test_vet_ns <- t.test(ds_vet_ns$defense_own)
vet_ns_mean <- test_vet_ns$estimate
vet_ns_lower <- test_vet_ns$conf.int[1]
vet_ns_upper <- test_vet_ns$conf.int[2]

# Veteran def
ds_vet_def <- subset(ds_r, treatment_group_f=="vet_def")
test_vet_def <- t.test(ds_vet_def$defense_own)
vet_def_mean <- test_vet_def$estimate
vet_def_lower <- test_vet_def$conf.int[1]
vet_def_upper <- test_vet_def$conf.int[2]

# Veteran pov
ds_vet_pov <- subset(ds_r, treatment_group_f=="vet_pov")
test_vet_pov <- t.test(ds_vet_pov$defense_own)
vet_pov_mean <- test_vet_pov$estimate
vet_pov_lower <- test_vet_pov$conf.int[1]
vet_pov_upper <- test_vet_pov$conf.int[2]

# Nonveteran National Security
ds_nonvet_ns <- subset(ds_r, treatment_group_f=="nonvet_ns")
test_nonvet_ns <- t.test(ds_nonvet_ns$defense_own)
nonvet_ns_mean <- test_nonvet_ns$estimate
nonvet_ns_lower <- test_nonvet_ns$conf.int[1]
nonvet_ns_upper <- test_nonvet_ns$conf.int[2]

# Nonveteran def
ds_nonvet_def <- subset(ds_r, treatment_group_f=="nonvet_def")
test_nonvet_def <- t.test(ds_nonvet_def$defense_own)
nonvet_def_mean <- test_nonvet_def$estimate
nonvet_def_lower <- test_nonvet_def$conf.int[1]
nonvet_def_upper <- test_nonvet_def$conf.int[2]

# Nonveteran pov
ds_nonvet_pov <- subset(ds_r, treatment_group_f=="nonvet_pov")
test_nonvet_pov <- t.test(ds_nonvet_pov$defense_own)
nonvet_pov_mean <- test_nonvet_pov$estimate
nonvet_pov_lower <- test_nonvet_pov$conf.int[1]
nonvet_pov_upper <- test_nonvet_pov$conf.int[2]


means <- c(vet_ns_mean, vet_def_mean, vet_pov_mean,
           nonvet_ns_mean, nonvet_def_mean, nonvet_pov_mean)
lower <- c(vet_ns_lower, vet_def_lower, vet_pov_lower,
           nonvet_ns_lower, nonvet_def_lower, nonvet_pov_lower)
upper <- c(vet_ns_upper, vet_def_upper, vet_pov_upper,
           nonvet_ns_upper, nonvet_def_upper, nonvet_pov_upper)
vet_group <- c("Veteran","Veteran", "Veteran", "Nonveteran","Nonveteran", "Nonveteran")
issue_group <- c("National Defense","Federal Deficit", "Poverty", "National Defense","Federal Deficit", "Poverty")
therm_condensed_d <- (cbind(means, lower, upper, vet_group, issue_group))
therm_condensed_d <- as.data.frame(therm_condensed_d)
therm_condensed_d$means <- as.numeric(as.character(therm_condensed_d$means))
therm_condensed_d$lower <- as.numeric(as.character(therm_condensed_d$lower))
therm_condensed_d$upper <- as.numeric(as.character(therm_condensed_d$upper))

ggplot(therm_condensed_d, aes(x=issue_group, y=means, color=vet_group)) +
  geom_point(size= 2, position=position_dodge(width = .6), stat="identity") +
  geom_errorbar(aes(ymin=lower, ymax=upper),
                width=.5,
                size = 1,
                position=position_dodge(.6)) +
  scale_x_discrete(guide = guide_axis(angle = 20)) +
        scale_y_continuous(limits=c(4.5, 9))+
  labs(title = "Republicans", x ="Issue Discussed", y = "")+
  scale_color_manual(values=c("gray0", "gray55"), labels = c("Nonveteran", "Veteran")) +
  theme_minimal()+
  #theme(legend.justification=c(0,0), legend.position=c(0,.8))+
  theme(legend.position="top")+
  theme(legend.title=element_blank()) +
  theme(text = element_text(size=16, family = ("serif"))) -> defense_own_fig_r
```

```{r Republicans Only Patriotism Fig}
# Veteran National Security
ds_vet_ns <- subset(ds_r, treatment_group_f=="vet_ns")
test_vet_ns <- t.test(ds_vet_ns$patriotism)
vet_ns_mean <- test_vet_ns$estimate
vet_ns_lower <- test_vet_ns$conf.int[1]
vet_ns_upper <- test_vet_ns$conf.int[2]

# Veteran def
ds_vet_def <- subset(ds_r, treatment_group_f=="vet_def")
test_vet_def <- t.test(ds_vet_def$patriotism)
vet_def_mean <- test_vet_def$estimate
vet_def_lower <- test_vet_def$conf.int[1]
vet_def_upper <- test_vet_def$conf.int[2]

# Veteran pov
ds_vet_pov <- subset(ds_r, treatment_group_f=="vet_pov")
test_vet_pov <- t.test(ds_vet_pov$patriotism)
vet_pov_mean <- test_vet_pov$estimate
vet_pov_lower <- test_vet_pov$conf.int[1]
vet_pov_upper <- test_vet_pov$conf.int[2]

# Nonveteran National Security
ds_nonvet_ns <- subset(ds_r, treatment_group_f=="nonvet_ns")
test_nonvet_ns <- t.test(ds_nonvet_ns$patriotism)
nonvet_ns_mean <- test_nonvet_ns$estimate
nonvet_ns_lower <- test_nonvet_ns$conf.int[1]
nonvet_ns_upper <- test_nonvet_ns$conf.int[2]

# Nonveteran def
ds_nonvet_def <- subset(ds_r, treatment_group_f=="nonvet_def")
test_nonvet_def <- t.test(ds_nonvet_def$patriotism)
nonvet_def_mean <- test_nonvet_def$estimate
nonvet_def_lower <- test_nonvet_def$conf.int[1]
nonvet_def_upper <- test_nonvet_def$conf.int[2]

# Nonveteran pov
ds_nonvet_pov <- subset(ds_r, treatment_group_f=="nonvet_pov")
test_nonvet_pov <- t.test(ds_nonvet_pov$patriotism)
nonvet_pov_mean <- test_nonvet_pov$estimate
nonvet_pov_lower <- test_nonvet_pov$conf.int[1]
nonvet_pov_upper <- test_nonvet_pov$conf.int[2]


means <- c(vet_ns_mean, vet_def_mean, vet_pov_mean,
           nonvet_ns_mean, nonvet_def_mean, nonvet_pov_mean)
lower <- c(vet_ns_lower, vet_def_lower, vet_pov_lower,
           nonvet_ns_lower, nonvet_def_lower, nonvet_pov_lower)
upper <- c(vet_ns_upper, vet_def_upper, vet_pov_upper,
           nonvet_ns_upper, nonvet_def_upper, nonvet_pov_upper)
vet_group <- c("Veteran","Veteran", "Veteran", "Nonveteran","Nonveteran", "Nonveteran")
issue_group <- c("National Defense","Federal Deficit", "Poverty", "National Defense","Federal Deficit", "Poverty")
therm_condensed_d <- (cbind(means, lower, upper, vet_group, issue_group))
therm_condensed_d <- as.data.frame(therm_condensed_d)
therm_condensed_d$means <- as.numeric(as.character(therm_condensed_d$means))
therm_condensed_d$lower <- as.numeric(as.character(therm_condensed_d$lower))
therm_condensed_d$upper <- as.numeric(as.character(therm_condensed_d$upper))

ggplot(therm_condensed_d, aes(x=issue_group, y=means, color=vet_group)) +
  geom_point(size= 2, position=position_dodge(width = .6), stat="identity") +
  geom_errorbar(aes(ymin=lower, ymax=upper),
                width=.5,
                size = 1,
                position=position_dodge(.6)) +
  scale_x_discrete(guide = guide_axis(angle = 20)) +
        scale_y_continuous(limits=c(5, 9))+
  labs(title= "Patriotism - Republicans", x ="Issue Discussed", y = "Mean Trait Rating \n")+
  scale_color_manual(values=c("gray0", "gray55"), labels = c("Nonveteran", "Veteran")) +
  theme_minimal()+
  #theme(legend.justification=c(0,0), legend.position=c(0,.8))+
  theme(legend.position="top")+
  theme(legend.title=element_blank()) +
  theme(text = element_text(size=16, family = ("serif"))) -> patriotism_fig_r
```

```{r Republicans Only Courage Fig}

# Veteran National Security
ds_vet_ns <- subset(ds_r, treatment_group_f=="vet_ns")
test_vet_ns <- t.test(ds_vet_ns$courage)
vet_ns_mean <- test_vet_ns$estimate
vet_ns_lower <- test_vet_ns$conf.int[1]
vet_ns_upper <- test_vet_ns$conf.int[2]

# Veteran def
ds_vet_def <- subset(ds_r, treatment_group_f=="vet_def")
test_vet_def <- t.test(ds_vet_def$courage)
vet_def_mean <- test_vet_def$estimate
vet_def_lower <- test_vet_def$conf.int[1]
vet_def_upper <- test_vet_def$conf.int[2]

# Veteran pov
ds_vet_pov <- subset(ds_r, treatment_group_f=="vet_pov")
test_vet_pov <- t.test(ds_vet_pov$courage)
vet_pov_mean <- test_vet_pov$estimate
vet_pov_lower <- test_vet_pov$conf.int[1]
vet_pov_upper <- test_vet_pov$conf.int[2]

# Nonveteran National Security
ds_nonvet_ns <- subset(ds_r, treatment_group_f=="nonvet_ns")
test_nonvet_ns <- t.test(ds_nonvet_ns$courage)
nonvet_ns_mean <- test_nonvet_ns$estimate
nonvet_ns_lower <- test_nonvet_ns$conf.int[1]
nonvet_ns_upper <- test_nonvet_ns$conf.int[2]

# Nonveteran def
ds_nonvet_def <- subset(ds_r, treatment_group_f=="nonvet_def")
test_nonvet_def <- t.test(ds_nonvet_def$courage)
nonvet_def_mean <- test_nonvet_def$estimate
nonvet_def_lower <- test_nonvet_def$conf.int[1]
nonvet_def_upper <- test_nonvet_def$conf.int[2]

# Nonveteran pov
ds_nonvet_pov <- subset(ds_r, treatment_group_f=="nonvet_pov")
test_nonvet_pov <- t.test(ds_nonvet_pov$courage)
nonvet_pov_mean <- test_nonvet_pov$estimate
nonvet_pov_lower <- test_nonvet_pov$conf.int[1]
nonvet_pov_upper <- test_nonvet_pov$conf.int[2]


means <- c(vet_ns_mean, vet_def_mean, vet_pov_mean,
           nonvet_ns_mean, nonvet_def_mean, nonvet_pov_mean)
lower <- c(vet_ns_lower, vet_def_lower, vet_pov_lower,
           nonvet_ns_lower, nonvet_def_lower, nonvet_pov_lower)
upper <- c(vet_ns_upper, vet_def_upper, vet_pov_upper,
           nonvet_ns_upper, nonvet_def_upper, nonvet_pov_upper)
vet_group <- c("Veteran","Veteran", "Veteran", "Nonveteran","Nonveteran", "Nonveteran")
issue_group <- c("National Defense","Federal Deficit", "Poverty", "National Defense","Federal Deficit", "Poverty")
therm_condensed_d <- (cbind(means, lower, upper, vet_group, issue_group))
therm_condensed_d <- as.data.frame(therm_condensed_d)
therm_condensed_d$means <- as.numeric(as.character(therm_condensed_d$means))
therm_condensed_d$lower <- as.numeric(as.character(therm_condensed_d$lower))
therm_condensed_d$upper <- as.numeric(as.character(therm_condensed_d$upper))

ggplot(therm_condensed_d, aes(x=issue_group, y=means, color=vet_group)) +
  geom_point(size= 2, position=position_dodge(width = .6), stat="identity") +
  geom_errorbar(aes(ymin=lower, ymax=upper),
                width=.5,
                size = 1,
                position=position_dodge(.6)) +
  scale_x_discrete(guide = guide_axis(angle = 20)) +
      scale_y_continuous(limits=c(5, 9))+
  labs(title= "Courage - Republicans", x ="Issue Discussed", y = "")+
  scale_color_manual(values=c("gray0", "gray55"), labels = c("Nonveteran", "Veteran")) +
  theme_minimal()+
  #theme(legend.justification=c(0,0), legend.position=c(0,.8))+
  theme(legend.position="top")+
  theme(legend.title=element_blank()) +
  theme(text = element_text(size=16, family = ("serif"))) -> courage_fig_r
```


```{r Republicans Only T-tests}

# Vets NS vs Def
ds_vets_1 <- subset(ds_r, treatment_group_f %in% c("vet_ns","vet_pov"))

t.test(ds_vets_1$therm ~ ds_vets_1$treatment_group_f) -> vets_therm_pov_rep
t.test(ds_vets_1$defense_own ~ ds_vets_1$treatment_group_f) -> vets_own_pov_rep
t.test(ds_vets_1$patriotism ~ ds_vets_1$treatment_group_f) -> vets_patriotism_pov_rep
t.test(ds_vets_1$courage ~ ds_vets_1$treatment_group_f) -> vets_courage_pov_rep

# Vets NS vs POV
ds_vets_2 <- subset(ds_r, treatment_group_f %in% c("vet_ns","vet_def"))

t.test(ds_vets_2$therm ~ ds_vets_2$treatment_group_f) -> vets_therm_def_rep
t.test(ds_vets_2$defense_own ~ ds_vets_2$treatment_group_f) -> vets_own_def_rep
t.test(ds_vets_2$patriotism ~ ds_vets_2$treatment_group_f) -> vets_patriotism_def_rep
t.test(ds_vets_2$courage ~ ds_vets_2$treatment_group_f) -> vets_courage_def_rep

# NS Vet vs Nonvets
ds_ns <- subset(ds_r, treatment_group_f %in% c("nonvet_ns","vet_ns"))

t.test(ds_ns$therm ~ ds_ns$treatment_group_f) -> ns_therm_rep
t.test(ds_ns$defense_own ~ ds_ns$treatment_group_f) -> ns_issue_own_rep
t.test(ds_ns$patriotism ~ ds_ns$treatment_group_f) -> ns_patriotism_rep
t.test(ds_ns$courage ~ ds_ns$treatment_group_f) -> ns_courage_rep

## Tables
# Set Function
pvalue_format <- function(x){
  z <- cut(as.numeric(x), breaks = c(-Inf, 0.01, 0.05, 0.1, Inf), labels = c("***", "**", "*", ""))
  as.character(z)
}


# Vets only Def
tab_vets_def_rep <- map_df(list(vets_therm_def_rep, vets_own_def_rep, vets_patriotism_def_rep, vets_courage_def_rep), .id = "dv", tidy)  
table_vets_def_rep <- tab_vets_def_rep[c("dv", "estimate", "estimate1", "estimate2", "p.value")]

  flextable(table_vets_def_rep,
    col_keys = c("dv", "estimate", "estimate1", "estimate2", "p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", estimate = "Diff. of Means",
                    estimate1 = "National Defense", estimate2 = "Deficit",
                    p.value = "Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableB4

# Vets only POV
tab_vets_pov_rep <- map_df(list(vets_therm_pov_rep, vets_own_pov_rep, vets_patriotism_pov_rep, vets_courage_pov_rep), .id = "dv", tidy)  
table_vets_pov_rep <- tab_vets_pov_rep[c("dv", "estimate", "estimate1", "estimate2", "p.value")]

  flextable(table_vets_pov_rep,
    col_keys = c("dv", "estimate", "estimate1", "estimate2", "p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", estimate = "Diff. of Means",
                    estimate1 = "National Defense", estimate2 = "Poverty",
                    p.value = "Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableB5
  
# National Security only 
tab_ns_rep <- map_df(list(ns_therm_rep, ns_issue_own_rep, ns_patriotism_rep, ns_courage_rep), .id = "dv", tidy)  
table_ns_rep <- tab_ns_rep[c("dv", "estimate", "estimate1", "estimate2", "p.value")]

  flextable(table_ns_rep,
    col_keys = c("dv", "estimate", "estimate1", "estimate2", "p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", estimate = "Diff. of Means",
                    estimate1 = "Veteran", estimate2 = "Nonveteran",
                    p.value = "Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableB6


## Ttests with Tukey correction

#remove nonvet pov and def
ds_anova <- subset(ds_r, treatment_group_f !="nonvet_pov" & treatment_group_f != "nonvet_def")

#therm
therm_anova <- aov(therm~treatment_group_f, data=ds_anova)
summary(therm_anova)
TukeyHSD(therm_anova, conf.level=.95) -> tukey_therm_rep

#defense own
defense_own_anova <- aov(defense_own~treatment_group_f, data=ds_anova)
summary(defense_own_anova)
TukeyHSD(defense_own_anova, conf.level=.95) -> tukey_def_own_rep

#patriotism
patriotism_anova <- aov(patriotism~treatment_group_f, data=ds_anova)
summary(patriotism_anova)
TukeyHSD(patriotism_anova, conf.level=.95) -> tukey_patriotism_rep

#courage
courage_anova <- aov(courage~treatment_group_f, data=ds_anova)
summary(courage_anova)
TukeyHSD(courage_anova, conf.level=.95) -> tukey_courage_rep

# Table
tukey_df_rep <- map_df(list(tukey_therm_rep, tukey_def_own_rep, tukey_patriotism_rep, tukey_courage_rep), .id = "dv", tidy)
table_tukey_rep <- tukey_df_rep[c("dv", "contrast", "estimate", "adj.p.value")]

  flextable(table_tukey_rep,
    col_keys = c("dv", "contrast", "estimate", "adj.p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(adj.p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", 
                    contrast = "Contrast", estimate = "Diff. of Means",
                    adj.p.value = "Adjusted Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "adj.p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableB11


```

```{r  Rep Only Models With Party Match}

mod_therm_pm_r <- lm(therm ~ treatment_group_f  +  partisan_match, ds_r)
summary(mod_therm_pm_r)

mod_defense_own_pm_r <- lm(defense_own ~ treatment_group_f + partisan_match, ds_r)
summary(mod_defense_own_pm_r)

mod_patriotism_pm_r <- lm(patriotism ~ treatment_group_f + partisan_match, ds_r)
summary(mod_patriotism_pm_r)

mod_courage_pm_r <- lm(courage ~ treatment_group_f + partisan_match, ds_r)
summary(mod_courage_pm_r)

# Table
models_rep <- list()
models_rep[['Feeling Thermometer']] <- mod_therm_pm_r
models_rep[['Defense Issue Ownership']] <- mod_defense_own_pm_r
models_rep[['Patriotism']] <- mod_patriotism_pm_r
models_rep[['Courage']] <- mod_courage_pm_r

coefs <- c("treatment_group_fnonvet_ns" = "Treatment: Nonvet. / Nat. Def.",
          "treatment_group_fvet_def" = "Treatment: Vet. / Deficit", 
          "treatment_group_fvet_pov" = "Treatment: Vet. / Poverty",
          "treatment_group_fnonvet_def" = "Treatment: Nonvet. / Deficit",
          "treatment_group_fnonvet_pov" = "Treatment: Nonvet. / Poverty",
          "partisan_match" = "Partisan Match",
          "(Intercept)" = "Constant")

modelsummary(models_rep, 
             stars = c('*' = .1, '**' = .05, '***' = .01),
             coef_map = coefs,
             title = '', #output = "Table_Rep.docx"
             ) -> TableB14

```


```{r Democrats Only Therm Fig}

ds_d <- ds %>%
  filter(ds$party_f == "Democrat")

# Veteran National Security
ds_d_vet_ns <- subset(ds_d, treatment_group_f=="vet_ns")
test_vet_ns <- t.test(ds_d_vet_ns$therm)
vet_ns_mean <- test_vet_ns$estimate
vet_ns_lower <- test_vet_ns$conf.int[1]
vet_ns_upper <- test_vet_ns$conf.int[2]

# Veteran def
ds_d_vet_def <- subset(ds_d, treatment_group_f=="vet_def")
test_vet_def <- t.test(ds_d_vet_def$therm)
vet_def_mean <- test_vet_def$estimate
vet_def_lower <- test_vet_def$conf.int[1]
vet_def_upper <- test_vet_def$conf.int[2]

# Veteran pov
ds_d_vet_pov <- subset(ds_d, treatment_group_f=="vet_pov")
test_vet_pov <- t.test(ds_d_vet_pov$therm)
vet_pov_mean <- test_vet_pov$estimate
vet_pov_lower <- test_vet_pov$conf.int[1]
vet_pov_upper <- test_vet_pov$conf.int[2]

# Nonveteran National Security
ds_d_nonvet_ns <- subset(ds_d, treatment_group_f=="nonvet_ns")
test_nonvet_ns <- t.test(ds_d_nonvet_ns$therm)
nonvet_ns_mean <- test_nonvet_ns$estimate
nonvet_ns_lower <- test_nonvet_ns$conf.int[1]
nonvet_ns_upper <- test_nonvet_ns$conf.int[2]

# Nonveteran def
ds_d_nonvet_def <- subset(ds_d, treatment_group_f=="nonvet_def")
test_nonvet_def <- t.test(ds_d_nonvet_def$therm)
nonvet_def_mean <- test_nonvet_def$estimate
nonvet_def_lower <- test_nonvet_def$conf.int[1]
nonvet_def_upper <- test_nonvet_def$conf.int[2]

# Nonveteran pov
ds_d_nonvet_pov <- subset(ds_d, treatment_group_f=="nonvet_pov")
test_nonvet_pov <- t.test(ds_d_nonvet_pov$therm)
nonvet_pov_mean <- test_nonvet_pov$estimate
nonvet_pov_lower <- test_nonvet_pov$conf.int[1]
nonvet_pov_upper <- test_nonvet_pov$conf.int[2]


means <- c(vet_ns_mean, vet_def_mean, vet_pov_mean,
           nonvet_ns_mean, nonvet_def_mean, nonvet_pov_mean)
lower <- c(vet_ns_lower, vet_def_lower, vet_pov_lower,
           nonvet_ns_lower, nonvet_def_lower, nonvet_pov_lower)
upper <- c(vet_ns_upper, vet_def_upper, vet_pov_upper,
           nonvet_ns_upper, nonvet_def_upper, nonvet_pov_upper)
vet_group <- c("Veteran","Veteran", "Veteran", "Nonveteran","Nonveteran", "Nonveteran")
issue_group <- c("National Defense","Federal Deficit", "Poverty", "National Defense","Federal Deficit", "Poverty")
therm_condensed_d <- (cbind(means, lower, upper, vet_group, issue_group))
therm_condensed_d <- as.data.frame(therm_condensed_d)
therm_condensed_d$means <- as.numeric(as.character(therm_condensed_d$means))
therm_condensed_d$lower <- as.numeric(as.character(therm_condensed_d$lower))
therm_condensed_d$upper <- as.numeric(as.character(therm_condensed_d$upper))

ggplot(therm_condensed_d, aes(x=issue_group, y=means, color=vet_group)) +
  geom_point(size= 2, position=position_dodge(width = .6), stat="identity") +
  geom_errorbar(aes(ymin=lower, ymax=upper),
                width=.5,
                size = 1,
                position=position_dodge(.6)) +
  scale_x_discrete(guide = guide_axis(angle = 20)) +
    scale_y_continuous(limits=c(55, 85))+
  labs(title = "Democrats", x ="", y = "")+
  scale_color_manual(values=c("gray0", "gray55"), labels = c("Nonveteran", "Veteran")) +
  theme_minimal()+
  #theme(legend.justification=c(0,0), legend.position=c(0,.8))+
  theme(legend.position="top")+
  theme(legend.title=element_blank()) +
  theme(text = element_text(size=16, family = ("serif"))) -> therm_fig_d
```


```{r Fig_6}

# Combine Party Figs
plot_grid(therm_fig, therm_fig_r, therm_fig_d, ncol = 3) # 4 x 8.75 PDF

```

```{r Democrats Only Defense Owenership Fig}

# Veteran National Security
ds_vet_ns <- subset(ds_d, treatment_group_f=="vet_ns")
test_vet_ns <- t.test(ds_vet_ns$defense_own)
vet_ns_mean <- test_vet_ns$estimate
vet_ns_lower <- test_vet_ns$conf.int[1]
vet_ns_upper <- test_vet_ns$conf.int[2]

# Veteran def
ds_vet_def <- subset(ds_d, treatment_group_f=="vet_def")
test_vet_def <- t.test(ds_vet_def$defense_own)
vet_def_mean <- test_vet_def$estimate
vet_def_lower <- test_vet_def$conf.int[1]
vet_def_upper <- test_vet_def$conf.int[2]

# Veteran pov
ds_vet_pov <- subset(ds_d, treatment_group_f=="vet_pov")
test_vet_pov <- t.test(ds_vet_pov$defense_own)
vet_pov_mean <- test_vet_pov$estimate
vet_pov_lower <- test_vet_pov$conf.int[1]
vet_pov_upper <- test_vet_pov$conf.int[2]

# Nonveteran National Security
ds_nonvet_ns <- subset(ds_d, treatment_group_f=="nonvet_ns")
test_nonvet_ns <- t.test(ds_nonvet_ns$defense_own)
nonvet_ns_mean <- test_nonvet_ns$estimate
nonvet_ns_lower <- test_nonvet_ns$conf.int[1]
nonvet_ns_upper <- test_nonvet_ns$conf.int[2]

# Nonveteran def
ds_nonvet_def <- subset(ds_d, treatment_group_f=="nonvet_def")
test_nonvet_def <- t.test(ds_nonvet_def$defense_own)
nonvet_def_mean <- test_nonvet_def$estimate
nonvet_def_lower <- test_nonvet_def$conf.int[1]
nonvet_def_upper <- test_nonvet_def$conf.int[2]

# Nonveteran pov
ds_nonvet_pov <- subset(ds_d, treatment_group_f=="nonvet_pov")
test_nonvet_pov <- t.test(ds_nonvet_pov$defense_own)
nonvet_pov_mean <- test_nonvet_pov$estimate
nonvet_pov_lower <- test_nonvet_pov$conf.int[1]
nonvet_pov_upper <- test_nonvet_pov$conf.int[2]


means <- c(vet_ns_mean, vet_def_mean, vet_pov_mean,
           nonvet_ns_mean, nonvet_def_mean, nonvet_pov_mean)
lower <- c(vet_ns_lower, vet_def_lower, vet_pov_lower,
           nonvet_ns_lower, nonvet_def_lower, nonvet_pov_lower)
upper <- c(vet_ns_upper, vet_def_upper, vet_pov_upper,
           nonvet_ns_upper, nonvet_def_upper, nonvet_pov_upper)
vet_group <- c("Veteran","Veteran", "Veteran", "Nonveteran","Nonveteran", "Nonveteran")
issue_group <- c("National Defense","Federal Deficit", "Poverty", "National Defense","Federal Deficit", "Poverty")
therm_condensed_d <- (cbind(means, lower, upper, vet_group, issue_group))
therm_condensed_d <- as.data.frame(therm_condensed_d)
therm_condensed_d$means <- as.numeric(as.character(therm_condensed_d$means))
therm_condensed_d$lower <- as.numeric(as.character(therm_condensed_d$lower))
therm_condensed_d$upper <- as.numeric(as.character(therm_condensed_d$upper))

ggplot(therm_condensed_d, aes(x=issue_group, y=means, color=vet_group)) +
  geom_point(size= 2, position=position_dodge(width = .6), stat="identity") +
  geom_errorbar(aes(ymin=lower, ymax=upper),
                width=.5,
                size = 1,
                position=position_dodge(.6)) +
  scale_x_discrete(guide = guide_axis(angle = 20)) +
        scale_y_continuous(limits=c(4.5, 9))+
  labs(title = "Democrats", x ="", y = "")+
  scale_color_manual(values=c("gray0", "gray55"), labels = c("Nonveteran", "Veteran")) +
  theme_minimal()+
  #theme(legend.justification=c(0,0), legend.position=c(0,.8))+
  theme(legend.position="top")+
  theme(legend.title=element_blank()) +
  theme(text = element_text(size=16, family = ("serif"))) -> defense_own_fig_d
```


```{r Fig_7}
# Combine Party Figs
plot_grid(defense_own_fig, defense_own_fig_r, defense_own_fig_d, ncol = 3) # 4 x 8.75 PDF
```


```{r Democrats Only T-tests}

# Vets NS vs POV
ds_vets_2 <- subset(ds_d, treatment_group_f %in% c("vet_ns","vet_pov"))

t.test(ds_vets_2$therm ~ ds_vets_2$treatment_group_f) -> vets_therm_pov_dem
t.test(ds_vets_2$defense_own ~ ds_vets_2$treatment_group_f) -> vets_own_pov_dem
t.test(ds_vets_2$patriotism ~ ds_vets_2$treatment_group_f) -> vets_patriotism_pov_dem
t.test(ds_vets_2$courage ~ ds_vets_2$treatment_group_f) -> vets_courage_pov_dem

# Vets NS vs Def
ds_vets_1 <- subset(ds_d, treatment_group_f %in% c("vet_ns","vet_def"))

t.test(ds_vets_1$therm ~ ds_vets_1$treatment_group_f) -> vets_therm_def_dem
t.test(ds_vets_1$defense_own ~ ds_vets_1$treatment_group_f) -> vets_own_def_dem
t.test(ds_vets_1$patriotism ~ ds_vets_1$treatment_group_f) -> vets_patriotism_def_dem
t.test(ds_vets_1$courage ~ ds_vets_1$treatment_group_f) -> vets_courage_def_dem

# NS Vet vs Nonvets
ds_ns <- subset(ds_d, treatment_group_f %in% c("nonvet_ns","vet_ns"))

t.test(ds_ns$therm ~ ds_ns$treatment_group_f) -> ns_therm_dem
t.test(ds_ns$defense_own ~ ds_ns$treatment_group_f) -> ns_issue_own_dem
t.test(ds_ns$patriotism ~ ds_ns$treatment_group_f) -> ns_patriotism_dem
t.test(ds_ns$courage ~ ds_ns$treatment_group_f) -> ns_courage_dem


## Tables
# Set Function
pvalue_format <- function(x){
  z <- cut(as.numeric(x), breaks = c(-Inf, 0.01, 0.05, 0.1, Inf), labels = c("***", "**", "*", ""))
  as.character(z)
}


# Vets only Def
tab_vets_def_dem <- map_df(list(vets_therm_def_dem, vets_own_def_dem, vets_patriotism_def_dem, vets_courage_def_dem), .id = "dv", tidy)  
table_vets_def_dem <- tab_vets_def_dem[c("dv", "estimate", "estimate1", "estimate2", "p.value")]

  flextable(table_vets_def_dem,
    col_keys = c("dv", "estimate", "estimate1", "estimate2", "p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", estimate = "Diff. of Means",
                    estimate1 = "National Defense", estimate2 = "Deficit",
                    p.value = "Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableB7

# Vets only POV
tab_vets_pov_dem <- map_df(list(vets_therm_pov_dem, vets_own_pov_dem, vets_patriotism_pov_dem, vets_courage_pov_dem), .id = "dv", tidy)  
table_vets_pov_dem <- tab_vets_pov_dem[c("dv", "estimate", "estimate1", "estimate2", "p.value")]

  flextable(table_vets_pov_dem,
    col_keys = c("dv", "estimate", "estimate1", "estimate2", "p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", estimate = "Diff. of Means",
                    estimate1 = "National Defense", estimate2 = "Poverty",
                    p.value = "Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableB8
  
# National Security only 
tab_ns_dem <- map_df(list(ns_therm_dem, ns_issue_own_dem, ns_patriotism_dem, ns_courage_dem), .id = "dv", tidy)  
table_ns_dem <- tab_ns_dem[c("dv", "estimate", "estimate1", "estimate2", "p.value")]

  flextable(table_ns_dem,
    col_keys = c("dv", "estimate", "estimate1", "estimate2", "p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", estimate = "Diff. of Means",
                    estimate1 = "Veteran", estimate2 = "Nonveteran",
                    p.value = "Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableB9

# Ttests with Tukey correction

#remove nonvet pov and def
ds_anova <- subset(ds_d, treatment_group_f !="nonvet_pov" & treatment_group_f != "nonvet_def")

#therm
therm_anova <- aov(therm~treatment_group_f, data=ds_anova)
summary(therm_anova)
TukeyHSD(therm_anova, conf.level=.95) -> tukey_therm_dem

#defense own
defense_own_anova <- aov(defense_own~treatment_group_f, data=ds_anova)
summary(defense_own_anova)
TukeyHSD(defense_own_anova, conf.level=.95) -> tukey_def_own_dem

#patriotism
patriotism_anova <- aov(patriotism~treatment_group_f, data=ds_anova)
summary(patriotism_anova)
TukeyHSD(patriotism_anova, conf.level=.95) -> tukey_patriotism_dem

#courage
courage_anova <- aov(courage~treatment_group_f, data=ds_anova)
summary(courage_anova)
TukeyHSD(courage_anova, conf.level=.95) -> tukey_courage_dem

# Table
tukey_df_dem <- map_df(list(tukey_therm_dem, tukey_def_own_dem, tukey_patriotism_dem, tukey_courage_dem), .id = "dv", tidy)
table_tukey_dem <- tukey_df_dem[c("dv", "contrast", "estimate", "adj.p.value")]

  flextable(table_tukey_dem,
    col_keys = c("dv", "contrast", "estimate", "adj.p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(adj.p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", 
                    contrast = "Contrast", estimate = "Diff. of Means",
                    adj.p.value = "Adjusted Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "adj.p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> TableB12

```

```{r  Democrats Only Models With Party Match}

mod_therm_pm_d <- lm(therm ~ treatment_group_f  +  partisan_match, ds_d)
summary(mod_therm_pm_d)

mod_defense_own_pm_d <- lm(defense_own ~ treatment_group_f + partisan_match, ds_d)
summary(mod_defense_own_pm_d)

mod_patriotism_pm_d <- lm(patriotism ~ treatment_group_f + partisan_match, ds_d)
summary(mod_patriotism_pm_d)

mod_courage_pm_d <- lm(courage ~ treatment_group_f + partisan_match, ds_d)
summary(mod_courage_pm_d)

# Table
models_dem <- list()
models_dem[['Feeling Thermometer']] <- mod_therm_pm_d
models_dem[['Defense Issue Ownership']] <- mod_defense_own_pm_d
models_dem[['Patriotism']] <- mod_patriotism_pm_d
models_dem[['Courage']] <- mod_courage_pm_d

coefs <- c("treatment_group_fnonvet_ns" = "Treatment: Nonvet. / Nat. Def.",
          "treatment_group_fvet_def" = "Treatment: Vet. / Deficit", 
          "treatment_group_fvet_pov" = "Treatment: Vet. / Poverty",
          "treatment_group_fnonvet_def" = "Treatment: Nonvet. / Deficit",
          "treatment_group_fnonvet_pov" = "Treatment: Nonvet. / Poverty",
          "partisan_match" = "Partisan Match",
          "(Intercept)" = "Constant")

modelsummary(models_dem, 
             stars = c('*' = .1, '**' = .05, '***' = .01),
             coef_map = coefs,
             title = '', #output = "Table_Dem.docx"
             ) -> TableB15


```


```{r Democrats Only Defense Ownership Fig}

# Veteran National Security
ds_vet_ns <- subset(ds_d, treatment_group_f=="vet_ns")
test_vet_ns <- t.test(ds_vet_ns$defense_own)
vet_ns_mean <- test_vet_ns$estimate
vet_ns_lower <- test_vet_ns$conf.int[1]
vet_ns_upper <- test_vet_ns$conf.int[2]

# Veteran def
ds_vet_def <- subset(ds_d, treatment_group_f=="vet_def")
test_vet_def <- t.test(ds_vet_def$defense_own)
vet_def_mean <- test_vet_def$estimate
vet_def_lower <- test_vet_def$conf.int[1]
vet_def_upper <- test_vet_def$conf.int[2]

# Veteran pov
ds_vet_pov <- subset(ds_d, treatment_group_f=="vet_pov")
test_vet_pov <- t.test(ds_vet_pov$defense_own)
vet_pov_mean <- test_vet_pov$estimate
vet_pov_lower <- test_vet_pov$conf.int[1]
vet_pov_upper <- test_vet_pov$conf.int[2]

# Nonveteran National Security
ds_nonvet_ns <- subset(ds_d, treatment_group_f=="nonvet_ns")
test_nonvet_ns <- t.test(ds_nonvet_ns$defense_own)
nonvet_ns_mean <- test_nonvet_ns$estimate
nonvet_ns_lower <- test_nonvet_ns$conf.int[1]
nonvet_ns_upper <- test_nonvet_ns$conf.int[2]

# Nonveteran def
ds_nonvet_def <- subset(ds_d, treatment_group_f=="nonvet_def")
test_nonvet_def <- t.test(ds_nonvet_def$defense_own)
nonvet_def_mean <- test_nonvet_def$estimate
nonvet_def_lower <- test_nonvet_def$conf.int[1]
nonvet_def_upper <- test_nonvet_def$conf.int[2]

# Nonveteran pov
ds_nonvet_pov <- subset(ds_d, treatment_group_f=="nonvet_pov")
test_nonvet_pov <- t.test(ds_nonvet_pov$defense_own)
nonvet_pov_mean <- test_nonvet_pov$estimate
nonvet_pov_lower <- test_nonvet_pov$conf.int[1]
nonvet_pov_upper <- test_nonvet_pov$conf.int[2]


means <- c(vet_ns_mean, vet_def_mean, vet_pov_mean,
           nonvet_ns_mean, nonvet_def_mean, nonvet_pov_mean)
lower <- c(vet_ns_lower, vet_def_lower, vet_pov_lower,
           nonvet_ns_lower, nonvet_def_lower, nonvet_pov_lower)
upper <- c(vet_ns_upper, vet_def_upper, vet_pov_upper,
           nonvet_ns_upper, nonvet_def_upper, nonvet_pov_upper)
vet_group <- c("Veteran","Veteran", "Veteran", "Nonveteran","Nonveteran", "Nonveteran")
issue_group <- c("National Defense","Federal Deficit", "Poverty", "National Defense","Federal Deficit", "Poverty")
therm_condensed_d <- (cbind(means, lower, upper, vet_group, issue_group))
therm_condensed_d <- as.data.frame(therm_condensed_d)
therm_condensed_d$means <- as.numeric(as.character(therm_condensed_d$means))
therm_condensed_d$lower <- as.numeric(as.character(therm_condensed_d$lower))
therm_condensed_d$upper <- as.numeric(as.character(therm_condensed_d$upper))

ggplot(therm_condensed_d, aes(x=issue_group, y=means, color=vet_group)) +
  geom_point(size= 2, position=position_dodge(width = .6), stat="identity") +
  geom_errorbar(aes(ymin=lower, ymax=upper),
                width=.5,
                size = 1,
                position=position_dodge(.6)) +
  scale_x_discrete(guide = guide_axis(angle = 20)) +
        scale_y_continuous(limits=c(5, 8.5))+
  labs(x ="Issue Discussed", y = "Mean Defense Ownership Rating \n")+
  scale_color_manual(values=c("gray0", "gray55"), labels = c("Nonveteran", "Veteran")) +
  theme_minimal()+
  #theme(legend.justification=c(0,0), legend.position=c(0,.8))+
  theme(legend.position="top")+
  theme(legend.title=element_blank()) +
  theme(text = element_text(size=16, family = ("serif"))) -> defense_own_fig_d
```

```{r Democrats Only Patriotism Fig}

# Veteran National Security
ds_vet_ns <- subset(ds_d, treatment_group_f=="vet_ns")
test_vet_ns <- t.test(ds_vet_ns$patriotism)
vet_ns_mean <- test_vet_ns$estimate
vet_ns_lower <- test_vet_ns$conf.int[1]
vet_ns_upper <- test_vet_ns$conf.int[2]

# Veteran def
ds_vet_def <- subset(ds_d, treatment_group_f=="vet_def")
test_vet_def <- t.test(ds_vet_def$patriotism)
vet_def_mean <- test_vet_def$estimate
vet_def_lower <- test_vet_def$conf.int[1]
vet_def_upper <- test_vet_def$conf.int[2]

# Veteran pov
ds_vet_pov <- subset(ds_d, treatment_group_f=="vet_pov")
test_vet_pov <- t.test(ds_vet_pov$patriotism)
vet_pov_mean <- test_vet_pov$estimate
vet_pov_lower <- test_vet_pov$conf.int[1]
vet_pov_upper <- test_vet_pov$conf.int[2]

# Nonveteran National Security
ds_nonvet_ns <- subset(ds_d, treatment_group_f=="nonvet_ns")
test_nonvet_ns <- t.test(ds_nonvet_ns$patriotism)
nonvet_ns_mean <- test_nonvet_ns$estimate
nonvet_ns_lower <- test_nonvet_ns$conf.int[1]
nonvet_ns_upper <- test_nonvet_ns$conf.int[2]

# Nonveteran def
ds_nonvet_def <- subset(ds_d, treatment_group_f=="nonvet_def")
test_nonvet_def <- t.test(ds_nonvet_def$patriotism)
nonvet_def_mean <- test_nonvet_def$estimate
nonvet_def_lower <- test_nonvet_def$conf.int[1]
nonvet_def_upper <- test_nonvet_def$conf.int[2]

# Nonveteran pov
ds_nonvet_pov <- subset(ds_d, treatment_group_f=="nonvet_pov")
test_nonvet_pov <- t.test(ds_nonvet_pov$patriotism)
nonvet_pov_mean <- test_nonvet_pov$estimate
nonvet_pov_lower <- test_nonvet_pov$conf.int[1]
nonvet_pov_upper <- test_nonvet_pov$conf.int[2]


means <- c(vet_ns_mean, vet_def_mean, vet_pov_mean,
           nonvet_ns_mean, nonvet_def_mean, nonvet_pov_mean)
lower <- c(vet_ns_lower, vet_def_lower, vet_pov_lower,
           nonvet_ns_lower, nonvet_def_lower, nonvet_pov_lower)
upper <- c(vet_ns_upper, vet_def_upper, vet_pov_upper,
           nonvet_ns_upper, nonvet_def_upper, nonvet_pov_upper)
vet_group <- c("Veteran","Veteran", "Veteran", "Nonveteran","Nonveteran", "Nonveteran")
issue_group <- c("National Defense","Federal Deficit", "Poverty", "National Defense","Federal Deficit", "Poverty")
therm_condensed_d <- (cbind(means, lower, upper, vet_group, issue_group))
therm_condensed_d <- as.data.frame(therm_condensed_d)
therm_condensed_d$means <- as.numeric(as.character(therm_condensed_d$means))
therm_condensed_d$lower <- as.numeric(as.character(therm_condensed_d$lower))
therm_condensed_d$upper <- as.numeric(as.character(therm_condensed_d$upper))

ggplot(therm_condensed_d, aes(x=issue_group, y=means, color=vet_group)) +
  geom_point(size= 2, position=position_dodge(width = .6), stat="identity") +
  geom_errorbar(aes(ymin=lower, ymax=upper),
                width=.5,
                size = 1,
                position=position_dodge(.6)) +
  scale_x_discrete(guide = guide_axis(angle = 20)) +
        scale_y_continuous(limits=c(5, 9))+
  labs(title= "Patriotism - Democrats", x ="Issue Discussed", y = "Mean Trait Rating \n")+
  scale_color_manual(values=c("gray0", "gray55"), labels = c("Nonveteran", "Veteran")) +
  theme_minimal()+
  #theme(legend.justification=c(0,0), legend.position=c(0,.8))+
  theme(legend.position="top")+
  theme(legend.title=element_blank()) +
  theme(text = element_text(size=16, family = ("serif"))) -> patriotism_fig_d
```

```{r Democrats Only Courage Fig}

# Veteran National Security
ds_vet_ns <- subset(ds_d, treatment_group_f=="vet_ns")
test_vet_ns <- t.test(ds_vet_ns$courage)
vet_ns_mean <- test_vet_ns$estimate
vet_ns_lower <- test_vet_ns$conf.int[1]
vet_ns_upper <- test_vet_ns$conf.int[2]

# Veteran def
ds_vet_def <- subset(ds_d, treatment_group_f=="vet_def")
test_vet_def <- t.test(ds_vet_def$courage)
vet_def_mean <- test_vet_def$estimate
vet_def_lower <- test_vet_def$conf.int[1]
vet_def_upper <- test_vet_def$conf.int[2]

# Veteran pov
ds_vet_pov <- subset(ds_d, treatment_group_f=="vet_pov")
test_vet_pov <- t.test(ds_vet_pov$courage)
vet_pov_mean <- test_vet_pov$estimate
vet_pov_lower <- test_vet_pov$conf.int[1]
vet_pov_upper <- test_vet_pov$conf.int[2]

# Nonveteran National Security
ds_nonvet_ns <- subset(ds_d, treatment_group_f=="nonvet_ns")
test_nonvet_ns <- t.test(ds_nonvet_ns$courage)
nonvet_ns_mean <- test_nonvet_ns$estimate
nonvet_ns_lower <- test_nonvet_ns$conf.int[1]
nonvet_ns_upper <- test_nonvet_ns$conf.int[2]

# Nonveteran def
ds_nonvet_def <- subset(ds_d, treatment_group_f=="nonvet_def")
test_nonvet_def <- t.test(ds_nonvet_def$courage)
nonvet_def_mean <- test_nonvet_def$estimate
nonvet_def_lower <- test_nonvet_def$conf.int[1]
nonvet_def_upper <- test_nonvet_def$conf.int[2]

# Nonveteran pov
ds_nonvet_pov <- subset(ds_d, treatment_group_f=="nonvet_pov")
test_nonvet_pov <- t.test(ds_nonvet_pov$courage)
nonvet_pov_mean <- test_nonvet_pov$estimate
nonvet_pov_lower <- test_nonvet_pov$conf.int[1]
nonvet_pov_upper <- test_nonvet_pov$conf.int[2]


means <- c(vet_ns_mean, vet_def_mean, vet_pov_mean,
           nonvet_ns_mean, nonvet_def_mean, nonvet_pov_mean)
lower <- c(vet_ns_lower, vet_def_lower, vet_pov_lower,
           nonvet_ns_lower, nonvet_def_lower, nonvet_pov_lower)
upper <- c(vet_ns_upper, vet_def_upper, vet_pov_upper,
           nonvet_ns_upper, nonvet_def_upper, nonvet_pov_upper)
vet_group <- c("Veteran","Veteran", "Veteran", "Nonveteran","Nonveteran", "Nonveteran")
issue_group <- c("National Defense","Federal Deficit", "Poverty", "National Defense","Federal Deficit", "Poverty")
therm_condensed_d <- (cbind(means, lower, upper, vet_group, issue_group))
therm_condensed_d <- as.data.frame(therm_condensed_d)
therm_condensed_d$means <- as.numeric(as.character(therm_condensed_d$means))
therm_condensed_d$lower <- as.numeric(as.character(therm_condensed_d$lower))
therm_condensed_d$upper <- as.numeric(as.character(therm_condensed_d$upper))

ggplot(therm_condensed_d, aes(x=issue_group, y=means, color=vet_group)) +
  geom_point(size= 2, position=position_dodge(width = .6), stat="identity") +
  geom_errorbar(aes(ymin=lower, ymax=upper),
                width=.5,
                size = 1,
                position=position_dodge(.6)) +
  scale_x_discrete(guide = guide_axis(angle = 20)) +
      scale_y_continuous(limits=c(5, 9))+
  labs(title= "Courage - Democrats", x ="Issue Discussed", y = "")+
  scale_color_manual(values=c("gray0", "gray55"), labels = c("Nonveteran", "Veteran")) +
  theme_minimal()+
  #theme(legend.justification=c(0,0), legend.position=c(0,.8))+
  theme(legend.position="top")+
  theme(legend.title=element_blank()) +
  theme(text = element_text(size=16, family = ("serif"))) -> courage_fig_d
```


```{r Figure 1B}

# Combine Trait Figs
plot_grid(patriotism_fig_r, courage_fig_r, patriotism_fig_d, courage_fig_d)
```

```{r  Models With Party Interaction}

# Models
mod_therm_i <- lm(therm ~ treatment_group_f * party2_f  +  partisan_match, ds)
summary(mod_therm_i)

mod_defense_own_i <- lm(defense_own ~ treatment_group_f * party2_f  + partisan_match, ds)
summary(mod_defense_own_i)

mod_patriotism_i <- lm(patriotism ~ treatment_group_f * party2_f  + partisan_match, ds)
summary(mod_patriotism_i)

mod_courage_i <- lm(courage ~ treatment_group_f * party2_f + partisan_match, ds)
summary(mod_courage_i)

# Table
models_int <- list()
models_int[['Feeling Thermometer']] <- mod_therm_i
models_int[['Defense Issue Ownership']] <- mod_defense_own_i
models_int[['Patriotism']] <- mod_patriotism_i
models_int[['Courage']] <- mod_courage_i

coefs <- c("treatment_group_fnonvet_ns" = "Treatment: Nonvet. / Nat. Def.",
          "treatment_group_fvet_def" = "Treatment: Vet. / Deficit", 
          "treatment_group_fnonvet_def" = "Treatment: Nonvet. / Deficit",
          "treatment_group_fvet_pov" = "Treatment: Vet. / Poverty", 
          "treatment_group_fnonvet_pov" = "Treatment: Nonvet. / Poverty",
          "party2_fRepublican" = "Respondent Party: Republican",
          "partisan_match" = "Partisan Match",
          "treatment_group_fnonvet_ns:party2_fRepublican" = "Nonvet. / Nat. Def. X Rep. Respondent",
          "treatment_group_fvet_def:party2_fRepublican" = "Vet. / Deficit X Rep. Respondent", 
          "treatment_group_fnonvet_def:party2_fRepublican" = "Nonvet. / Deficit X Rep. Respondent",
          "treatment_group_fvet_pov:party2_fRepublican" = "Vet. / Poverty X Rep. Respondent", 
          "treatment_group_fnonvet_pov:party2_fRepublican" = "Nonvet. / Poverty X Rep. Respondent",
          "(Intercept)" = "Constant")

modelsummary(models_int, 
             stars = c('*' = .1, '**' = .05, '***' = .01),
             coef_map = coefs,
             title = '', #output = "Table_int_exp3.docx"
             ) -> TableB16

```